package com.stage.helper;

import com.stage.annotation.Column;
import com.stage.annotation.Label;

import java.util.Scanner;

/**
 * Created by Stage on 2017/3/17.
 */
public class InputManager {
    Scanner input;

    public InputManager() {
        input = new Scanner(System.in);
    }

    /**
     * 通过Label标签来输入对象
     *
     * @param type
     * @param label
     * @return
     */
    public Object inputObjectByLabel(String type, Label label) {
        switch (type) {
            case "java.lang.String":
                return inputStringByLabel(label);
            case "java.lang.Integer":
                return inputIntegerByLabel(label);
            case "java.lang.Boolean":
                return inputBooleanByLabel(label);
        }
        return null;
    }

    public Object inputObjectByColumn(String type, Column column) {
        switch (type) {
            case "java.lang.String":
                return inputStringByColumn(column);
            case "java.lang.Integer":
                return inputIntegerByColumn(column);
            case "java.lang.Boolean":
                return inputBooleanByColumn(column);
        }
        return null;
    }

    private Boolean inputBooleanByColumn(Column column) {
        return inputBoolean(column.value().value());
    }

    private Integer inputIntegerByColumn(Column column) {
        return inputInteger(column.value().value(), column.minValue(), column.maxValue());
    }

    private String inputStringByColumn(Column column) {
        return inputString(column.value().value(), column.minLength(), column.maxLength(), null);
    }

    /**
     * 通过Label输入String
     *
     * @param label
     * @return
     */
    public String inputStringByLabel(Label label) {
        return inputString(label.value());
    }

    /**
     * 输入string
     *
     * @param tip
     * @param minLen
     * @param maxLen
     * @param selectValues
     * @return
     */
    public String inputString(String tip, int minLen, int maxLen, String[] selectValues) {
        String result;
        System.out.print(tip);
        result = input.nextLine();
        while (isStringOk(result, minLen, maxLen, null)) {
            result = input.nextLine();
        }
        return result;
    }

    /**
     * 显示tip输入string
     *
     * @param tip
     * @return
     */
    public String inputString(String tip) {
        String result = null;
        System.out.println(tip);
        while (true) {
            try {
                result = input.nextLine();
                break;
            } catch (Exception e) {
                System.out.println("您的输入有误，请重新输入！");
                result = input.nextLine();
            }
        }
        return result;
    }

    public Integer inputIntegerByLabel(Label label) {
        return inputInteger(label.value());
    }

    public Integer inputInteger(String tip) {
        System.out.print(tip);
        Integer result = null;
        while (true) {
            try {
                result = input.nextInt();
                input.nextLine();
                break;
            } catch (Exception e) {
                System.out.println("您的输入有误，请重新输入");
                input.nextLine();
            }
        }
        return result;
    }

    public Integer inputInteger(String tip, int minValue, int maxValue) {
        System.out.print(tip);
        Integer result;
        result = input.nextInt();
        while (isIntegerOk(result, minValue, maxValue)) {
            while (true) {
                try {
                    result = input.nextInt();
                    input.nextLine();
                    break;
                } catch (Exception e) {
                    System.out.println("您的输入有误，请重新输入");
                    input.nextLine();
                }
            }
        }
        return result;
    }

    public Boolean inputBooleanByLabel(Label label) {
        return inputBoolean(label.value());
    }

    public Boolean inputBoolean(String tip) {
        System.out.print(tip);
        Boolean result = null;
        while (true) {
            try {
                result = input.nextBoolean();
                input.nextLine();
                break;
            } catch (Exception e) {
                System.out.println("您的输入有误，请重新输入");
                input.nextLine();
            }
        }

        return result;
    }

    public String firstCharToUp(String str) {
        char ch[] = str.toCharArray();
        char ch1 = Character.toUpperCase(ch[0]);
        ch[0] = ch1;
        String s = new String(ch);
        return s;
    }

    public Boolean isStringOk(String userInput, int minLen, int maxLen, String[] selectValues) {
        if (selectValues != null) {
            //
        }
        if (userInput.toCharArray().length < minLen || userInput.toCharArray().length > maxLen) {
            System.out.println("您的输入有误");
            return true;
        }
        return false;
    }

    public Boolean isIntegerOk(Integer userInput, int minValue, int maxValue) {
        if (userInput < minValue || userInput > maxValue) {
            System.out.println("您的输入有误");
            return true;
        }
        return false;
    }
}
